/**
 * 
 */
package com.flashdeck.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.flashdeck.R;
import com.flashdeck.domain.Feedback;

/**
 * @author Vincentas
 * 
 */
public class FeedbackActivity extends Activity {

	private Button sendButton;
	
	private EditText subjectEditText;
	
	private EditText messageEditText;
	
	private static final int FEEDBACK_SENT = 0;
	
	private static final int FEEDBACK_FAILED = 1;
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.feedback_layout);
		
		sendButton = (Button) findViewById(R.id.sendButton);
		subjectEditText = (EditText) findViewById(R.id.subjectEditText);
		messageEditText = (EditText) findViewById(R.id.messageEditText);
		
		sendButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				Feedback feedback = new Feedback(
					subjectEditText.getText().toString(), 
					messageEditText.getText().toString());
				
				final Feedback.Sender[] senders = new Feedback.Sender[1];
				
				final ProgressDialog dialog = ProgressDialog.show(
					FeedbackActivity.this, 
					getString(R.string.feedback_sending_title), 
					getString(R.string.feedback_sending_message), 
					true, 
					true , 
					new DialogInterface.OnCancelListener() {
						
						@Override
						public void onCancel(DialogInterface dialog) {
							Toast.makeText(FeedbackActivity.this, R.string.feedback_cancel_message, Toast.LENGTH_SHORT).show();
							Feedback.Sender sender = senders[0];
							if (sender != null) {
								sender.cancel();
							}
						}
				
					});
				
				final Handler handler = new Handler() {

					/* (non-Javadoc)
					 * @see android.os.Handler#handleMessage(android.os.Message)
					 */
					@Override
					public void handleMessage(Message msg) {
						if (msg.what == FEEDBACK_SENT) {
							dialog.dismiss();
							Toast.makeText(FeedbackActivity.this, R.string.feedback_sent_message, Toast.LENGTH_SHORT).show();
							FeedbackActivity.this.finish();
						} else if (msg.what == FEEDBACK_FAILED) {
							dialog.dismiss();
							Toast.makeText(FeedbackActivity.this, R.string.feedback_error_message, Toast.LENGTH_LONG).show();
						} else {
							super.handleMessage(msg);
						}
					}			
					
				};
				
				senders[0] = feedback.send(new Feedback.ProgressListener() {
					
					@Override
					public void onSuccess() {
						handler.sendEmptyMessage(FEEDBACK_SENT);
					}
					
					@Override
					public void onFailure() {
						handler.sendEmptyMessage(FEEDBACK_FAILED);		
					}
				});
			}
			
		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onBackPressed()
	 */
	@Override
	public void onBackPressed() {
		if (subjectEditText.getText().toString().length() != 0 ||
				messageEditText.getText().toString().length() != 0) {
			new AlertDialog.Builder(this).
				setIcon(android.R.drawable.ic_dialog_alert).
				setTitle(R.string.feedback_quit_title).
				setMessage(R.string.feedback_quit_message).
				setPositiveButton(android.R.string.yes, 
						new DialogInterface.OnClickListener() {
			
							@Override
							public void onClick(DialogInterface dialog,	int which) {
								FeedbackActivity.this.finish();
							}
						}).
				setNegativeButton(android.R.string.no, null).
				show();
		} else {
			FeedbackActivity.this.finish();
		}
	}
}
